package com.java.userservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.userservice.dao.RoleMapper;
import com.java.userservice.dao.RolePermissionMapper;
import com.java.userservice.model.Permission;
import com.java.userservice.model.Role;
import com.java.userservice.model.RolePermission;
import com.java.userservice.model.dto.RolePermissionDTO;
import com.java.userservice.model.vo.TreeVo;
import com.java.userservice.service.PermissionService;
import com.java.userservice.service.RolePermissionService;
import com.java.userservice.service.RoleService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public Result list(Role role) {
        PageHelper.startPage(role.getPageNum(), role.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据

        List<Role> list = roleMapper.list(role);
        PageInfo<Role> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        roleMapper.get(id);
        return Result.success();
    }

    @Override
    public Result save(Role role) {
        roleMapper.save(role);
        return Result.success();
    }

    @Override
    public Result modify(Role role) {
        roleMapper.modify(role);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            roleMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.listAll();
    }

    @Override
    public Result listPermissionByRoleId(Integer id) {
        List<RolePermission> list = rolePermissionService.list(id);
        List<Integer> permissionIds = list.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        return Result.success(permissionIds);
    }

    @Transactional
    @Override
    public Result savePermission(RolePermissionDTO rolePermissionDTO) {
        return rolePermissionService.save(rolePermissionDTO);
    }

    @Override
    public Result listPermission() {
        List<TreeVo> list = new ArrayList<>();

        // 查询权限
        List<Permission> permissions = permissionService.listAll();
        if (!CollectionUtils.isEmpty(permissions)) {

            // 第一级菜单
            for (Permission permission : permissions) {
                if (permission.getParentId() == null) {
                    TreeVo treeVo = new TreeVo();
                    treeVo.setId(permission.getId());
                    treeVo.setLabel(permission.getAuthName());
                    list.add(treeVo);
                }
            }

            Map<Integer, TreeVo> parentMap = list.stream()
                    .collect(Collectors.toMap(treeVo -> treeVo.getId(), treeVo -> treeVo));

            // 第二级菜单
            for (Permission permission : permissions) {
                if (permission.getParentId() != null) {
                    TreeVo treeVo = new TreeVo();
                    treeVo.setId(permission.getId());
                    treeVo.setLabel(permission.getAuthName());
                    parentMap.get(permission.getParentId()).getChildren().add(treeVo);
                }
            }
        }

        return Result.success(list);
    }

}
